part of '../../repository.dart';

const _kGooglePlayPurchasedSubscriptionAutoRenewalEnabledKey = 'auto_renewal_enabled';
const _kGooglePlayPurchasedSubscriptionStatusKey = 'status';
const _kGooglePlayPurchasedSubscriptionExpirationTimeKey = 'expiration_time';
const _kGooglePlayPurchasedSubscriptionCancellationInfoKey = 'cancellation_info';
const _kGooglePlayPurchasedSubscriptionPriceChangeStatusKey = 'price_change_status';
const _kGooglePlayPurchasedSubscriptionPauseInfoKey = 'pause_info';
const _kGooglePlayPurchasedSubscriptionTransactionsKey = 'transactions';

const _kGooglePlayPurchasedSubscriptionTypeValue = 'purchased_subscription';

class _GooglePlayPurchasedSubscriptionDto extends Equatable implements _GooglePlayPurchasedItemDto {
  const _GooglePlayPurchasedSubscriptionDto._internal({
    required this.purchaseItemSku,
    required this.purchaseToken,
    required this.autoRenewalEnabled,
    required this.statusDto,
    required this.expirationTimeDto,
    required this.cancellationInfoDto,
    required this.pauseInfoDto,
    required this.priceChangeStatusDto,
    required this.transactionDtos,
  });

  @override
  final String purchaseItemSku;

  @override
  final String purchaseToken;

  final bool autoRenewalEnabled;
  final _GooglePlayPurchasedSubscriptionStatusDto statusDto;
  final ApiTimeDto? expirationTimeDto;
  final _GooglePlayPurchasedSubscriptionCancellationInfoDto? cancellationInfoDto;
  final _GooglePlayPurchasedSubscriptionPauseInfoDto? pauseInfoDto;
  final _GooglePlayPurchasedSubscriptionPriceChangeStatusDto priceChangeStatusDto;
  final BuiltList<_GooglePlayPurchasedSubscriptionTransactionDto> transactionDtos;

  @override
  List<Object?> get props {
    return [
      purchaseItemSku,
      purchaseToken,
      autoRenewalEnabled,
      statusDto,
      expirationTimeDto,
      cancellationInfoDto,
      pauseInfoDto,
      priceChangeStatusDto,
      transactionDtos,
    ];
  }

  factory _GooglePlayPurchasedSubscriptionDto.fromJson(Map<String, dynamic> json) {
    final statusString = json[_kGooglePlayPurchasedSubscriptionStatusKey] as String;
    final statusDto = _GooglePlayPurchasedSubscriptionStatusDto.fromString(statusString);

    final expirationTimeString =
        json[_kGooglePlayPurchasedSubscriptionExpirationTimeKey] as String?;

    ApiTimeDto? expirationTimeDto;

    if (expirationTimeString != null) {
      expirationTimeDto = ApiTimeDto.fromString(expirationTimeString);
    }

    final cancellationInfoJson =
        json[_kGooglePlayPurchasedSubscriptionCancellationInfoKey] as Map<String, dynamic>?;

    _GooglePlayPurchasedSubscriptionCancellationInfoDto? cancellationInfoDto;

    if (cancellationInfoJson != null) {
      cancellationInfoDto = _GooglePlayPurchasedSubscriptionCancellationInfoDtoFactory()
          .fromJson(cancellationInfoJson);
    }

    final pauseInfoJson =
        json[_kGooglePlayPurchasedSubscriptionPauseInfoKey] as Map<String, dynamic>?;

    _GooglePlayPurchasedSubscriptionPauseInfoDto? pauseInfoDto;

    if (pauseInfoJson != null) {
      pauseInfoDto = _GooglePlayPurchasedSubscriptionPauseInfoDto.fromJson(pauseInfoJson);
    }

    final priceChangeStatusString =
        json[_kGooglePlayPurchasedSubscriptionPriceChangeStatusKey] as String;
    final priceChangeStatusDto =
        _GooglePlayPurchasedSubscriptionPriceChangeStatusDto.fromString(priceChangeStatusString);

    final transactionMaps =
        List.castFrom<dynamic, Map>(json[_kGooglePlayPurchasedSubscriptionTransactionsKey]);

    final transactionDtos = transactionMaps.map((transactionMap) {
      final transactionJson = Map.castFrom<dynamic, dynamic, String, dynamic>(transactionMap);
      return _GooglePlayPurchasedSubscriptionTransactionDto.fromJson(transactionJson);
    }).toBuiltList();

    return _GooglePlayPurchasedSubscriptionDto._internal(
      purchaseItemSku: json[_kGooglePlayPurchasedItemPurchaseItemSkuKey],
      purchaseToken: json[_kGooglePlayPurchasedItemPurchaseTokenKey],
      autoRenewalEnabled: json[_kGooglePlayPurchasedSubscriptionAutoRenewalEnabledKey],
      statusDto: statusDto,
      expirationTimeDto: expirationTimeDto,
      cancellationInfoDto: cancellationInfoDto,
      pauseInfoDto: pauseInfoDto,
      priceChangeStatusDto: priceChangeStatusDto,
      transactionDtos: transactionDtos,
    );
  }

  @override
  GooglePlayPurchasedSubscription toEntity() {
    final transactions = transactionDtos.map((transactionDto) {
      return transactionDto.toEntity();
    }).toBuiltList();

    return GooglePlayPurchasedSubscription(
      purchaseItemSku: purchaseItemSku,
      purchaseToken: purchaseToken,
      autoRenewalEnabled: autoRenewalEnabled,
      status: statusDto.toEntity(),
      expirationTime: expirationTimeDto?.toEntity(),
      pauseInfo: pauseInfoDto?.toEntity(),
      priceChangeStatus: priceChangeStatusDto.toEntity(),
      cancellationInfo: cancellationInfoDto?.toEntity(),
      transactions: transactions,
    );
  }
}
